/**
 * 
 */
package com.oracle.jimu.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.oracle.jimu.bean.Comment;
import com.oracle.jimu.bean.UserBean;

/**
 * @author Administrator
 *
 */
public class UserDaoImpl implements UserDao {
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/* (non-Javadoc)
	 * @see com.oracle.jimu.dao.UserDao#getUser()
	 */
	@Override
	public List<UserBean> getUser() {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		String hql="from com.oracle.jimu.bean.UserBean";
		Query q=session.createQuery(hql);
		List list = q.list();		
		System.out.println("list.size()="+list);
		
		return list;
	}


	/* (non-Javadoc)
	 * @see com.oracle.jimu.dao.UserDao#deleteUser(java.lang.Integer)
	 */
	@Override
	public void deleteUser(Integer id) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		UserBean u=(UserBean) session.get(UserBean.class, id);
		session.delete(u);
	}

	/* (non-Javadoc)
	 * @see com.oracle.jimu.dao.UserDao#updateUser(com.oracle.jimu.bean.UserBean)
	 */
	@Override
	public void updateUser(UserBean user) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		session.update(user);
		
	}

	@Override
	public UserBean getUserById(Integer id) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		String hql="from com.oracle.jimu.bean.UserBean where id= "+id;
		Query q=session.createQuery(hql);
		List list=q.list();
		UserBean user=(UserBean) list.get(0);
		return user;
	}

	@Override
	public List<UserBean> getUserByName(String name) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		String hql="from com.oracle.jimu.bean.UserBean where name like %" + name + "%";
		Query q=session.createQuery(hql);
		
		List list = q.list();		
		System.out.println("list.size()="+list);
		
		return list;
	}

	@Override
	public void userregister(UserBean user) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(user);
		
	}

	@Override
	public UserBean loader(String name, String password) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql = "from com.oracle.jimu.bean.UserBean a where a.name=? And a.password=?";
		Query q = session.createQuery(hql);
		q.setString(0,name);
		q.setString(1,password);
		List l = q.list();
		if(l.size()>0) return (UserBean) l.get(0);
		return null;
	}

	@Override
	public UserBean findUserByName(String name) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql="from com.oracle.jimu.bean.UserBean where name=:name";
		Query q = session.createQuery(hql);
		q.setString("name", name);
		List l = q.list();
		if(l.size()>0) return (UserBean) l.get(0);
		return null;
	}

	@Override
	public void saveComment(Comment comment) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		session.save(comment);
	}
  
	

}
